package gov.pbc.nn.kjgl.item.dao;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.bstek.bdf2.core.business.IDept;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;

import gov.pbc.nn.core.bdf.service.DeptService;
import gov.pbc.nn.core.dao.AbstractHibernateDao;
import gov.pbc.nn.kjgl.entity.Project;
import gov.pbc.nn.kjgl.entity.ProjectFile;

/**
 * 项目变更申请书Dao
 * 
 * @author jack.zhao
 * 
 */
@Component
public class ProjectItemDao extends AbstractHibernateDao {
	@Autowired
	@Qualifier(DeptService.BEAN_ID)
	private DeptService deptService;

	public void getPageAll(Page<Project> page, Criteria criteria) {
		DetachedCriteria query = this.buildDetachedCriteria(criteria, Project.class);
		query.add(Restrictions.or(Restrictions.eq("invalid", false), Restrictions.isNull("invalid")));
		query.add(Restrictions.eq("state", 10));
		this.pagingQuery(page, query);
	}

	public void deleteProjectFile(ProjectFile pf) throws IOException {
		Path path = Paths.get(pf.getPath());
		this.delete(pf);
		Files.deleteIfExists(path);
	}

	public boolean ajaxCheckKj(String username) {
		List<IDept> loadUserDept = deptService.loadUserDepts(username);
		if (loadUserDept.size() > 0) {
			String deptName = loadUserDept.get(0).getName();
			return deptName.contains("科技");
		}
		return false;
	}
}
